﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CloudClass.Models;
using Orchard.Data;

namespace CloudClass.Services
{
    public class LessonService:ILessonService
    {
        private readonly IRepository<Lesson> _lessonRepository;
        //private readonly IEnumerable<ICarEventHandler> _handlers; 
        public LessonService(IRepository<Lesson> lessonRepository)
        {
            _lessonRepository = lessonRepository;
            //_handlers = handlers;
        }

        public virtual bool Create(Models.Lesson model)
        {

            try
            {
                _lessonRepository.Create(model);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public virtual bool Edit(Lesson model)
        {
            try
            {
                _lessonRepository.Update(model);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public virtual bool Delete(int[] array)
        {
            try
            {
                foreach (var id in array)
                {
                    var item = this.Get(id);
                    _lessonRepository.Delete(item);
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        public virtual Lesson Get(int id)
        {
            var item = _lessonRepository.Get(id);
            return item;
        }

        public virtual List<Lesson> GetList()
        {
            return _lessonRepository.Table.ToList();
        }

        public virtual object List(out int count)
        {
            var list = _lessonRepository.Table.ToList();
            count = list.Count;
            return list;
        }

        /// <summary>
        /// 根据课程编号获取课时列表
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public virtual List<Lesson> GetListByCourseId(int Id)
        {
            return _lessonRepository.Table.ToList().Where(p=>p.CourseInfo.Id==Id).OrderBy(o=>o.LessonOrder).ToList();
        }
    }
}